package com.slashmobility.finappsparty.parser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.slashmobility.finapps.model.DataModelIBEX35;
import com.slashmobility.finapps.model.DataModelValue;


/**
 * Parser para convertir de XML a Modelo de Datos
 */

public class DataModelIBEX35SaxParser extends DefaultHandler {

	private StringBuilder builder = new StringBuilder();
	private DataModelIBEX35 dataModelIBEX35 = null;
	private DataModelValue dataModelValue = null;
	private int actualFieldId = -1;

	public void startDocument() throws SAXException {
		dataModelIBEX35 = new DataModelIBEX35();
		dataModelValue = new DataModelValue();
	}

	public void endDocument() throws SAXException {

	}

	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		int length = atts.getLength();
		if (localName.equalsIgnoreCase("field")) {
			for (int i = 0; i < length; i++) {
				String atributeName = atts.getQName(i);
				String value = atts.getValue(i);

				if (atributeName.equals("id")) {
					int idField = Integer.parseInt(value); //TODO: puede devolver un N/A ?
					actualFieldId = idField;
				}
			}
		}
	}

	
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equalsIgnoreCase("row")) {
			// dataModelIBEX35.setPaquete(builder.toString());

			dataModelIBEX35.putValue(dataModelValue.getCodigo(),dataModelValue);
			dataModelValue = new DataModelValue();
		} else if (localName.equalsIgnoreCase("field")) {
			updateDataModelValue();
			actualFieldId = -1;

		}
		builder.setLength(0);
	}

	private void updateDataModelValue() {

		switch (actualFieldId) {
		case -1:
			break;

		case 0:
			dataModelValue.setCodigo(builder.toString().replaceAll("\t", "").replaceAll("\n", ""));
			break;

		case 41:
			dataModelValue.setDescripcion(builder.toString().replaceAll("\t", "").replaceAll("\n", ""));
			break;

		case 1:
			dataModelValue.setUlt(builder.toString().replaceAll("\t", "").replaceAll("\n", ""));
			int signo=(int) Math.pow(-1,(int)(Math.random()*10) % 2);		
			float currentValue=Float.parseFloat(dataModelValue.getUlt())+(float)Math.random()*signo;
			dataModelValue.setUlt(currentValue+"");
			break;

		case 6:
			 signo=(int) Math.pow(-1,(int)(Math.random()*10) % 2);	
			dataModelValue.setDif(builder.toString().replaceAll("\t", "").replaceAll("\n", ""));	
			currentValue=Float.parseFloat(dataModelValue.getDif())+(float)Math.random()*signo;
			dataModelValue.setDif(currentValue+"");
			
			break;

		case 7:
			signo=(int) Math.pow(-1,(int)(Math.random()*10) % 2);	
			dataModelValue.setPercent(builder.toString().replaceAll("\t", "").replaceAll("\n", ""));
			currentValue=Float.parseFloat(dataModelValue.getPercent())+(float)Math.random()*signo;
			dataModelValue.setPercent(currentValue+"");
			break;
		case 25:
			dataModelValue.setAnt(builder.toString().replaceAll("\t", "").replaceAll("\n", ""));
			break;
		case 3340:
			dataModelValue.setRentabilidad(builder.toString().replaceAll("\t", "").replaceAll("\n", ""));
			break;
		case 14:
			dataModelValue.setVolumen(builder.toString().replaceAll("\t", "").replaceAll("\n", ""));
			break;		
		case 18:
			dataModelValue.setMax(builder.toString().replaceAll("\t", "").replaceAll("\n", ""));
			break;
		case 17:
			dataModelValue.setMin(builder.toString().replaceAll("\t", "").replaceAll("\n", ""));
			break;			
		case 5:
			dataModelValue.setHora(builder.toString().replaceAll("\t", "").replaceAll("\n", ""));
			break;
		}
	}

	public DataModelIBEX35 getDataModelIBEX35() {
		return dataModelIBEX35;
	}

	public void setDataModelIBEX35(DataModelIBEX35 dataModelIBEX35) {
		this.dataModelIBEX35 = dataModelIBEX35;
	}

	public void characters(char ch[], int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		builder.append(ch, start, length);
		
	}

}
